package com.demo.controller;

import com.alibaba.fastjson.JSON;
import com.demo.annotation.AuthPassport;
import com.demo.enums.SignTypeEnum;
import com.demo.model.ApiParamsForm;
import com.demo.model.ApiParamsJson;
import com.demo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @AuthPassport(signType = SignTypeEnum.NOT_SIGN)
    @RequestMapping(value = "/testGet",method = RequestMethod.GET)
    public Object testGet(@RequestParam(value = "id")Integer id,
                          @RequestParam(value = "name")String name){
        log.info("测试get请求，id={}, name={}",id,name);
        return ok("你好，id为"+id+"的"+name);
    }

    @RequestMapping(value = "/testPostForm",method = RequestMethod.POST)
    public Object testPostForm(ApiParamsForm params){
        UserVO userVO = JSON.parseObject(params.getBody(),UserVO.class);
        log.info("测试form形式提交的post请求，userVO={}",userVO);
        return ok(new UserVO()
                .setId(userVO.getId()+100)
                .setName(userVO.getName()+"form"));
    }

    @AuthPassport(signType = SignTypeEnum.JSON)
    @RequestMapping(value = "/testPostJson",method = RequestMethod.POST)
    public Object testPostJson(@RequestBody ApiParamsJson<UserVO> params){
        UserVO userVO = params.getBody();
        log.info("测试json形式提交的post请求，userVO={}",userVO);
        return ok(new UserVO()
                .setId(userVO.getId()+1000)
                .setName(userVO.getName()+"json"));
    }
}
